/**
 * Copyright (c) 2024 - CARThunder
 * All Rights Reserved by Chengdu CARThunder Intelligent Technology Co., LTD and its affiliates.
 * You may not use, copy, distribute, modify, transmit in any form this file
 * except in compliance with CARThunder in writing by applicable law.
 *
 */

/**
 * file description.
 * @file    filename
 * @brief   brief function description.
 * @details detailed function description.
 * @version 1.0
 * @author  author_name
 * @date    last_modified_date
 *
 *      Edit History
 *      ----------------------------------------------------------------------------
 *      DATE                     NAME               DESCRIPTION
 *      2024-06-20              author_name         Create it.
 *
 */


package com.cart.musicdemo.adapter;


import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.cart.musicdemo.R;
import com.cart.musicdemo.base.utils.StringUtils;
import com.cart.musicdemo.model.SongModel;
import java.util.List;

public class SongAdapter extends RecyclerView.Adapter<SongAdapter.ViewHolder> {

    private List<SongModel> songList;
    private OnItemClickListener listener;


    public interface OnItemClickListener {
        void onItemClick(int position,boolean reset);
    }

    public SongAdapter(List<SongModel> data, OnItemClickListener item) {
        this.songList = data;
        this.listener = item;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_songs_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        //字符判定
        holder.textView.setText(
                StringUtils.subString(
                        songList.get(position).getName(), songList.get(position).getSinger()));
        holder.ivSongImage.setTag(R.id.ivSongImage, position);
        //播放图标
        if (position == (int) holder.ivSongImage.getTag(R.id.ivSongImage)
                && songList.get(position).getPlaying()) {
            holder.ivSongImage.setImageResource(R.drawable.ic_baseline_headset_24);
        } else {
            holder.ivSongImage.setImageResource(R.drawable.ic_baseline_music_note_24);
        }
        //列表绑定监听
        holder.relativeLayout.setOnClickListener(v -> listener.onItemClick(position,false));
    }

    @Override
    public int getItemCount() {
        return songList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView textView;

        private RelativeLayout relativeLayout;

        private ImageView ivSongImage;

        public ViewHolder(View itemView) {
            super(itemView);
            ivSongImage = itemView.findViewById(R.id.ivSongImage);
            relativeLayout = itemView.findViewById(R.id.rl_click);
            textView = itemView.findViewById(R.id.tvSongName);

        }
    }

}